package com.aaa.ni.util;

import com.aaa.common.util.Constants;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * fileName:GlobalExceptionHandler
 * description:
 *
 * @author:wzl createTime:2020/9/4 9:12
 */
@RestControllerAdvice
public class GlobalExceptionHandler{

    /**
     * 异常处理方法  Exception 默认处理Exception及其子孙类
     * @param exception 拿到异常对象
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result handerException(Exception exception){
        //判断是否是自定义异常
        if (exception instanceof CustomException){
            CustomException exception1 = (CustomException)exception;
            return new Result(exception1.getErrorCode(),exception1.getErrorMessage());
        }
        //exception.getClass().getName() 通过反射拿到异常类名称
        //不是自定义异常
        return new Result(Constants.RESULT.INNERERROR,Constants.RESULT.IEMESSAGE,exception.getClass().getName());
    }
}










